package com.example.app6;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;


import androidx.appcompat.app.AppCompatActivity;


import java.util.ArrayList;
import java.util.List;

public class UCActivity3 extends AppCompatActivity {


    private FrameLayout frameLayoutContainer;
    private List<ImageView> imageViews;
    private int currentImageIndex = 0;
    private static final long SLIDESHOW_INTERVAL = 3000; // 轮播间隔，单位毫秒 (例如3秒)
    private Handler slideshowHandler;
    private Runnable slideshowRunnable;


    private final int[] imageViewIds = {
            R.id.imageView1,
            R.id.imageView2,
            R.id.imageView3,
            R.id.imageView4,
            R.id.imageView5
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ucactivity3); // 关联XML布局

        frameLayoutContainer = findViewById(R.id.frameLayoutContainer);
        imageViews = new ArrayList<>();

        // 初始化ImageView列表
        for (int id : imageViewIds) {
            ImageView imageView = findViewById(id);
            if (imageView != null) {
                imageViews.add(imageView);
            }
        }

        if (imageViews.isEmpty()) {
            // 处理没有找到ImageView的情况，例如显示错误信息或不启动轮播
            return;
        }

        // 确保初始只有第一张图片可见 (虽然XML中已经设置，但作为保险)
        for (int i = 0; i < imageViews.size(); i++) {
            imageViews.get(i).setVisibility(i == currentImageIndex ? View.VISIBLE : View.GONE);
        }

        slideshowHandler = new Handler(Looper.getMainLooper());
        slideshowRunnable = new Runnable() {
            @Override
            public void run() {
                if (imageViews.isEmpty()) return;

                // 隐藏当前图片
                imageViews.get(currentImageIndex).setVisibility(View.GONE);

                // 计算下一张图片的索引
                currentImageIndex = (currentImageIndex + 1) % imageViews.size();

                // 显示下一张图片
                imageViews.get(currentImageIndex).setVisibility(View.VISIBLE);
                // 如果需要动态改变图片源，可以在这里设置
                // imageViews.get(currentImageIndex).setImageResource(imageResIds[currentImageIndex]);


                // 再次调度以实现循环轮播
                slideshowHandler.postDelayed(this, SLIDESHOW_INTERVAL);
            }
        };
    }

    @Override
    protected void onResume() {
        super.onResume();
        startSlideshow(); // Activity 可见时开始轮播
    }

    @Override
    protected void onPause() {
        super.onPause();
        stopSlideshow(); // Activity 不可见时停止轮播，防止内存泄漏
    }

    private void startSlideshow() {
        if (slideshowHandler != null && slideshowRunnable != null && imageViews.size() > 1) {
            // 移除之前可能存在的旧回调，避免重复执行
            slideshowHandler.removeCallbacks(slideshowRunnable);
            // 延迟 SLIDESHOW_INTERVAL 后开始执行第一次切换
            slideshowHandler.postDelayed(slideshowRunnable, SLIDESHOW_INTERVAL);
        }
    }

    private void stopSlideshow() {
        if (slideshowHandler != null && slideshowRunnable != null) {
            slideshowHandler.removeCallbacks(slideshowRunnable);
        }
    }
}